package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mqx
 * @date 2020-7-24 14:02:40
 */
@Api(tags = "商品属性接口")
@RestController // @ResponseBody + @Controller
@RequestMapping("admin/product")
public class BaseManageController {

    @Autowired
    private ManageService manageService;

    // http://api.gmall.com/admin/product/getCategory1
    // Result 返回结果集类
    // 获取一级分类数据
    @GetMapping("getCategory1")
    public Result getCategory1(){
        List<BaseCategory1> category1List = manageService.getCategory1();
        // Result.ok(new ArrayList<>());== 给集合数据赋值给 Result.data 属性
        // Result.setData(new ArrayList<>());  Result.data = new ArrayList<>()
        // return Result.ok(new ArrayList<>());
        return Result.ok(category1List);
    }
    // http://api.gmall.com/admin/product/getCategory2/{category1Id}
    // 获取二级分类数据
    @GetMapping("getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){
        List<BaseCategory2> category2List = manageService.getCategory2(category1Id);
        // 封装的返回结果集
        return Result.ok(category2List);
    }

    // http://api.gmall.com/admin/product/getCategory3/{category2Id}
    // 获取三级分类数据
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){
        List<BaseCategory3> category3List = manageService.getCategory3(category2Id);
        // 封装的返回结果集
        return Result.ok(category3List);
    }

    // http://api.gmall.com/admin/product/attrInfoList/{category1Id}/{category2Id}/{category3Id}
    // 根据分类Id 获取平台属性集合
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable Long category1Id,
                               @PathVariable Long category2Id,
                               @PathVariable Long category3Id){

        List<BaseAttrInfo> attrInfoList = manageService.getAttrInfoList(category1Id, category2Id, category3Id);
        return Result.ok(attrInfoList);
    }

    // http://api.gmall.com/admin/product/saveAttrInfo
    // 根据api 接口文档可以看出,前台传递过来的json 字符串转化为BaseAttrInfo
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        // 调用服务层数据
        manageService.saveAttrInfo(baseAttrInfo);
        // 返回
        return  Result.ok();
    }

    // 根据平台属性id 获取平台属性值集合
    // http://api.gmall.com/admin/product/getAttrValueList/{attrId}
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable Long attrId){

        // 修改时,必须要先确定是否有这个属性.如果有这个属性,我们需要从这个属性中来获取平台属性值集合.
        // 上述描述从业务角度来讲会更加严谨!
        // attrId = baseAttrInfo.id
        BaseAttrInfo baseAttrInfo = manageService.getBaseAttrInfo(attrId);
        if (null != baseAttrInfo){
            // 从平台属性中自动获取到平台属性值集合
            List<BaseAttrValue> baseAttrValueList = baseAttrInfo.getAttrValueList();
            // 返回数据
            return Result.ok(baseAttrValueList);
        }

        return Result.fail();


        // 这个方案:功能可以实现! 但是,不够严谨!
        // 根据当前的平台属性Id  attrId = baseAttrInfo.id
        // select * from base_attr_value where attr_id = attrId
        // List<BaseAttrValue> baseAttrValueList = manageService.getAttrValueList(attrId);
        // 返回数据
        // return Result.ok(baseAttrValueList);
    }


}
